package cluster

import (
	"context"
	"gitee/yinlai/kms-backend/config"
	"gitee/yinlai/kms-backend/utils/logs"
	"github.com/gin-gonic/gin"
	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"net/http"
)

func Get(r *gin.Context) {
	logs.Debug(nil, "获取集群详情")

	// 获取一个集群信息的详情
	clusterId := r.Query("id")
	//返回数据
	returnData := config.ReturnData{}

	// 获取集群的配置/secret
	clusterSecret, err := config.InClusterClientSet.CoreV1().Secrets(config.MetadataNamespace).Get(context.TODO(), clusterId, v1.GetOptions{})
	if err != nil {
		// 说明查询集群失败
		logs.Error(map[string]interface{}{"clusterId": clusterId, "msg": err.Error()}, "查询集群详情失败")
		returnData.Status = 400
		returnData.Message = "获取集群详情失败： " + err.Error()
		return
	} else {
		returnData.Status = 200
		returnData.Message = "查询成功"
		returnData.Data = make(map[string]interface{})

		// 处理kubeconfig字段时base64加密的，单独抽离出来处理
		clusterConfigMap := clusterSecret.Annotations
		clusterConfigMap["kubeconfig"] = string(clusterSecret.Data["kubeconfig"])

		returnData.Data["item"] = clusterConfigMap
	}
	r.JSON(http.StatusOK, returnData)
}
